<?php 
namespace app\common\model;
class Poster extends Common{
	protected $insert = ['addtime' => SYS_TIME];
	protected function getAddtimeAttr($value){
		return date("Y-m-d H:i:s",$value);
	}
	protected function getRealnameAttr($value,$data){
		return model('AdminUser')->where('userid', $data['userid'])->value('realname');
	}
	protected function getOrigTitleAttr($value,$data){
		return $data['origid'] ? $this->where('id', $data['origid'])->value('title') : '原创海报';
	}
	protected function getStatusnameAttr($value, $data){
		$status = [0=>'正常',1=>'禁用'];
		return $status[$data['status']];
	}
	protected function getTitlesAttr($value, $data){
		if ($data['origid']) {
			$more_title = $this->where('id', $data['origid'])->value('more_title');
			return $more_title ? explode("\n", $more_title) : [];
		}
		return $data['more_title'] ? explode("\n", $data['more_title']) : [];
	}
	protected function getAlldataAttr($value, $data){
		$arr = [0=>'正常',1=>'禁用'];
		$data['statusname'] = $arr[$data['status']];
		$data['realname'] = model('AdminUser')->where('userid', $data['userid'])->value('realname');
		$data['addtime'] = date("Y-m-d H:i:s", $data['addtime']);
		return $data;
	}
	protected function getThumbAttr($value, $data){
		if(strpos($value,"http://") !== false){
			return $value;
		}
		else{
			return $value ? APP_URL.'/'.$value : '';
		}
	}
	protected function getThumbnailAttr($value, $data){
		return $data['thumb'] ? APP_URL.thumb($data['thumb'], 800, 800, 0) : '';
	}
	public function delete_data($data){
		$ids = $data['item_ids'];
		$idss = explode(',', $ids);
		foreach ($idss as $v) {
			db('DealerContent')->where('typeid', 2)->where('contentid', $v)->delete();
		}
		return $this::destroy($ids);
	}
	public function list_datas($data, $uinfo){
		$page = isset($data['page']) ? $data['page'] : 1;
    	$typeid = isset($data['typeid']) ? $data['typeid'] : 1;
    	$filtid = isset($data['filtid']) ? $data['filtid'] : 0;
    	$where['typeid'] = $typeid;
		$today = date('Y-m-d', SYS_TIME);
		$where['startdate'] = ['elt', $today];
		$where['enddate'] = ['egt', $today];
		if ($filtid < 0) {
			$roleid = -$filtid;
			$where['roleid'] = $roleid;
			if ($uinfo['roleid'] == 5) {
				if ($roleid == 4) {
					$where['userid'] = $uinfo['puserid'];
					$where['roleid'] = 4;
				}else{
					$userlist = model('AdminUser')->where('puserid', $uinfo['puserid'])->where('roleid', 6)->select();
					$userids = [];
					foreach ($userlist as $v) {
						$userids[] = $v['userid'];
					}
					$where['userid'] = ['in', $userids];
				}
			}else{
				$dealer_id = model('AdminUser')->get_userid($uinfo['puserid'], 6);
				$postlist = db('DealerContent')->where('typeid', 2)->where('dealer_id', $dealer_id)->where('roleid', $roleid)->select();
				$postids = [];
				foreach ($postlist as $v) {
					$postids[] = $v['contentid'];
				}
				$where['id'] = ['in', $postids];
			}
		}elseif ($filtid > 0) {
			$where['roleid'] = 6;
			$where['userid'] = $filtid;
		}else{
			$userids = [];
			if ($uinfo['roleid'] == 5) {
				$userlist = model('AdminUser')->where('puserid', $uinfo['puserid'])->where('roleid', 6)->select();
				$userids = [$uinfo['puserid']];
				foreach ($userlist as $v) {
					$userids[] = $v['userid'];
				}
				$where['userid'] = ['in', $userids];
			}else{
				$dealer_id = model('AdminUser')->get_userid($uinfo['puserid'], 6);
				$postlist = db('DealerContent')->where('typeid', 2)->where('dealer_id', $dealer_id)->select();
				$postids = [];
				foreach ($postlist as $v) {
					$postids[] = $v['contentid'];
				}
				$where['id'] = ['in', $postids];
			}
		}

    	$list = $this->list_datad($where, $page, 10);
    	$lists = ['top'=>[], 'list'=>[]];
    	foreach ($list as $v) {
    		$v['editurl'] = $v['typeid'] == 2 ? $v['url'] : APP_URL.'/index/member/add_poster/posterid/'.$v['id'];
    		$v['sharethumb'] = APP_URL.thumb($v['thumb'], 50, 50);
    		$v['thumbnail'] = $v['thumbnail'];
    		if ($v['top']) {
    			$lists['top'][] = $v;
    		}else{
    			$lists['list'][] = $v;
    		}
    	}
    	return $lists;
	}
}